package cn.jc.domain.service.impl;

import cn.jc.common.entity.PageResult;
import cn.jc.common.util.IdWorkerUtil;
import cn.jc.common.util.LoginUtil;
import cn.jc.domain.convert.SubjectInfoConverter;
import cn.jc.domain.entity.SubjectInfoBO;
import cn.jc.domain.redis.RedisUtil;
import cn.jc.domain.service.SubjectInfoDomainService;
import cn.jc.infra.basic.basic.dao.SubjectBriefDao;
import cn.jc.infra.basic.basic.entity.SubjectInfo;
import cn.jc.infra.basic.basic.entity.SubjectInfoEs;
import cn.jc.infra.basic.basic.entity.SubjectLabel;
import cn.jc.infra.basic.basic.entity.SubjectMapping;
import cn.jc.infra.basic.basic.service.SubjectEsService;
import cn.jc.infra.basic.basic.service.SubjectInfoService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.ClassFileVersion;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Required
    private SubjectInfoService subjectInfoService;
    @Required
    private SubjectEsService subjectEsService;

    @Resource
    private RedisUtil redisUtil;

    private static final String RANK_KEY = "subject_rank";

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {

        if (log.isInfoEnabled()) {
            log.info("SubjectInfoDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectInfoBO));
        }
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToInfo(subjectInfoBO);
        subjectInfoService.insert(subjectInfo);
         //同步到es
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setDocId(new IdWorkerUtil(1, 1, 1).nextId());
        subjectInfoEs.setSubjectId(subjectInfo.getId());
        subjectInfoEs.setSubjectAnswer(subjectInfoBO.getSubjectAnswer());
        subjectInfoEs.setCreateTime(new Date().getTime());
        subjectInfoEs.setCreateUser("鸡翅");
        subjectInfoEs.setSubjectName(subjectInfo.getSubjectName());
        subjectInfoEs.setSubjectType(subjectInfo.getSubjectType());
        subjectEsService.insert(subjectInfoEs);
        //redis放入zadd计入排行榜
        redisUtil.addScore(RANK_KEY, LoginUtil.getLoginId(), 1);
    }
    @Override
    public PageResult<SubjectInfoBO> getSubjectPage(SubjectInfoBO subjectInfoBO) {
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToInfo(subjectInfoBO);
        int count = subjectInfoService.countByCondition(subjectInfo, subjectInfoBO.getCategoryId()
                , subjectInfoBO.getLabelId());
        if (count == 0) {
            return pageResult;
        }
        List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo, subjectInfoBO.getCategoryId()
                , subjectInfoBO.getLabelId(), start, subjectInfoBO.getPageSize());
        List<SubjectInfoBO> subjectInfoBOS = SubjectInfoConverter.INSTANCE.convertListInfoToBO(subjectInfoList);
        subjectInfoBOS.forEach(info -> {
            SubjectMapping subjectMapping = new SubjectMapping();
            subjectMapping.setSubjectId(info.getId());
        });
        pageResult.setRecords(subjectInfoBOS);
        pageResult.setTotal(count);
        return pageResult;
    }
//
//    @Override
//    public SubjectInfoBO querySubjectInfo(SubjectInfoBO subjectInfoBO) {
//        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBO.getId());
//        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
//        SubjectOptionBO optionBO = handler.query(subjectInfo.getId().intValue());
//        SubjectInfoBO bo = SubjectInfoConverter.INSTANCE.convertOptionAndInfoToBo(optionBO, subjectInfo);
//        SubjectMapping subjectMapping = new SubjectMapping();
//        subjectMapping.setSubjectId(subjectInfo.getId());
//        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
//        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
//        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());
//        bo.setLabelName(labelNameList);
//        bo.setLiked(subjectLikedDomainService.isLiked(subjectInfoBO.getId().toString(), LoginUtil.getLoginId()));
//        bo.setLikedCount(subjectLikedDomainService.getLikedCount(subjectInfoBO.getId().toString()));
//        assembleSubjectCursor(subjectInfoBO, bo);
//        return bo;
//    }
}
